package com.jiawa.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiawa.train.business.domain.po.Train;
import com.jiawa.train.business.mapper.TrainMapper;
import com.jiawa.train.business.req.TrainQueryReq;
import com.jiawa.train.business.req.TrainSaveReq;
import com.jiawa.train.business.resp.TrainQueryResp;
import com.jiawa.train.business.service.ITrainService;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 车次 服务实现类
 * </p>
 *
 * @author hrz
 * @since 2025-09-07
 */
@Service
public class TrainServiceImpl extends ServiceImpl<TrainMapper, Train> implements ITrainService {

    @Override
    public void save(TrainSaveReq req) {
        Train train = BeanUtil.copyProperties(req, Train.class);
        //检查是否已存在该条目,从而判断是更新还是增加
        if(ObjectUtil.isNull(train.getId())){
            Train one = this.lambdaQuery().eq(Train::getCode, train.getCode()).one();
            if(ObjectUtil.isNotEmpty(one) || one != null){
                //1.先检查车次号是否已存在
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CODE_UNIQUE_ERROR);
            }
            //2.新增
            train.setCreateTime(LocalDateTime.now());
            train.setUpdateTime(LocalDateTime.now());
            this.save(train);
        }else{
            //3.更新
            train.setUpdateTime(LocalDateTime.now());
            this.updateById(train);
        }

    }

    @Override
    public PageResp<TrainQueryResp> queryList(TrainQueryReq req) {
        //1.分页查询数据
        Page<Train> page = this.lambdaQuery()
                .orderByDesc(Train::getCreateTime)
                .page(new Page<>(req.getPage(), req.getSize()));
        //2.转成VO返回
        List<Train> records = page.getRecords();
        if(CollUtil.isEmpty(records)){
            PageResp<TrainQueryResp> objectPageResp = new PageResp<>();
            objectPageResp.setList(new ArrayList<>());
            objectPageResp.setTotal(0L);
            return objectPageResp;
        }
        List<TrainQueryResp> list = BeanUtil.copyToList(records, TrainQueryResp.class);
        PageResp<TrainQueryResp> objectPageResp = new PageResp<>();
        objectPageResp.setList(list);
        objectPageResp.setTotal(page.getTotal());
        return objectPageResp;
    }

    @Override
    public void delete(Long id) {
        //1.校验参数
        if(ObjectUtil.isEmpty(id)){
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        }
        //2.删除
        baseMapper.deleteById(id);
    }

    @Override
    public List<TrainQueryResp> queryAll() {
        List<Train> list = this.lambdaQuery()
                .orderByAsc(Train::getCode)
                .list();
        return BeanUtil.copyToList(list, TrainQueryResp.class);
    }

    @Override
    public List<Train> selectAll() {
        return this.lambdaQuery().list();
    }
}
